package com.top.horizon.th1804app.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.net.protocol.model.FileTag;
import com.top.horizon.th1804app.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class TagAdapter extends RecyclerView.Adapter<TagAdapter.TagViewHolder> {

    private List<FileTag.Tag> mData = new ArrayList<>();
    private Context mContext;
    private ItemClickListener mListener;

    public TagAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<FileTag.Tag> tags) {
        if (tags == null || tags.size() == 0) {
            return;
        }
        mData = tags;
        notifyDataSetChanged();
    }

    public void setListener(ItemClickListener itemClickListener){
        mListener = itemClickListener;
    }

    @NonNull
    @Override
    public TagViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new TagViewHolder(View.inflate(mContext, R.layout.tag_item, null));
    }

    @Override
    public void onBindViewHolder(@NonNull TagViewHolder holder, int position) {
        holder.setData(position,mData.get(position));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class TagViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        public TagViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView;
        }

        public void setData(final int position, final FileTag.Tag tag){
            RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            textView.setLayoutParams(layoutParams);
            textView.setText(tag.tag);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null){
                        mListener.onClick(position,tag);
                    }
                }
            });
        }
    }

    public interface ItemClickListener{
        void onClick(int position, FileTag.Tag tag);
    }
}
